package com.zhudi.advice;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Modifier;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

@Component
@Aspect
@Slf4j
public class UserAdvice {

    @Pointcut("execution(public * com.zhudi.service.*.*(..))")
    private void pt(){}

    @AfterReturning(value = "pt()",returning = "serviceResult")
    public void after(JoinPoint joinPoint,Object serviceResult){
        //todo:修改为输出到日志文件
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        log.debug("目标方法执行时间:"+LocalDateTime.now().format(formatter));
        log.debug("目标方法所属类的简单类名:" +        joinPoint.getSignature().getDeclaringType().getSimpleName());
        log.debug("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName());
        log.debug("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers()));
        log.debug("目标方法名为:" + joinPoint.getSignature().getName());
        log.debug("目标方法参数值集合："+ Arrays.asList(joinPoint.getArgs()));
//        log.debug("目标方法返回值为："+serviceResult.toString());
    }
}
